/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.hiside.android.travel.dashboard;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.hiside.android.travel.R;
import org.hiside.android.travel.dashboard.view.DrawView;
import org.springframework.android.showcase.AbstractAsyncActivity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class DashboardActivity extends AbstractAsyncActivity {

    protected static final String TAG = DashboardActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard_main);
        init();
    }

    private void init() {
        LinearLayout layout = (LinearLayout) findViewById(R.id.root);
        final DrawView view = new DrawView(this);
        view.setMinimumHeight(1500);
        view.setMinimumWidth(600);
        // 通知view组件重绘
        view.invalidate();
        layout.addView(view);
        String fileName = "sightaddress_sightname_user_20121121";
//        saveMyBitmap(view, fileName, 0);
    }

    public void saveMyBitmap(ImageView view, String fileName, int percent) {

        Bitmap bmp = drawable2Bitmap(view.getDrawable());
        String path = Environment.getExternalStorageDirectory().getPath() + "/hitravel/" + fileName + ".png";
        Log.i(TAG, path);
        File f = new File(path);
        try {
            f.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bmp.compress(Bitmap.CompressFormat.JPEG, percent, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 附加drawable2Bitmap方法
    static Bitmap drawable2Bitmap(Drawable d) {
        int width = d.getIntrinsicWidth();
        int height = d.getIntrinsicHeight();
        Bitmap.Config config = d.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap(width, height, config);
        Canvas canvas = new Canvas(bitmap);
        d.setBounds(0, 0, width, height);
        d.draw(canvas);
        return bitmap;
    }
}
